package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.common.service.SysAuthorityService;
import cn.e4j.modular.crm.entity.GwbsT;
import cn.e4j.modular.crm.mapper.GwbsTMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuw
 * @since 2019-09-02
 */
@Service
public class GwbsTServiceImpl extends ServiceImpl<GwbsTMapper, GwbsT> {

    @Autowired
    public SysAuthorityService sysAuthorityService;

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwbslist(Page<Object> page, Dto dto) {
        //客户权限
        dto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(dto));
        //部门权限
        dto.put("ORGS", sysAuthorityService.getOrgsAuthority(dto));
//        List<Map<String, Object>> gwbsDtos = this.baseMapper.getListData(page, dto);
        List<Map<String, Object>> gwbsDtos = InvokeUtil.invokeBeanMethod("gwbsTMapper,getListData",page,dto);
        return gwbsDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwbglist(Page<Object> page, Dto dto) {
        if("true".equals(dto.getString("permission"))){
            //部门权限
            dto.put("ORGS", sysAuthorityService.getOrgsAuthority(dto));

            // 业务员权限
            Dto sys004= sysAuthorityService.getSys004(sysAuthorityService.getOrgsAuthority(dto));
            dto.put("ywy",sys004.get("ywy"));
        }
        List<Map<String, Object>> gwbgDtos = this.baseMapper.getGwbgList(page, dto);
        return gwbgDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwaglist(Page<Object> page, Dto dto) {
        List<Map<String, Object>> gwagDtos = this.baseMapper.getGwaglist(page, dto);
        return gwagDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwadlist(Page<Object> page, Dto dto) {
        List<Map<String, Object>> gwadDtos = this.baseMapper.getGwadlist(page, dto);
        return gwadDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwaolist(Page<Object> page, Dto dto) {
        List<Map<String, Object>> gwaoDtos = this.baseMapper.getGwaolist(page, dto);
        return gwaoDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwablist(Page<Object> page, Dto dto) {
        List<Map<String, Object>> gwabDtos = this.baseMapper.getGwablist(page, dto);
        return gwabDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getOrgslist(Page<Object> page, Dto dto) {
        List<Map<String, Object>> orgDtos = this.baseMapper.getOrgslist(dto);
        if(orgDtos.size() > 0){
            page.setTotal(Long.valueOf(orgDtos.get(0).get("count").toString()));
        }
        return orgDtos;
    }

    @Transactional
    public void add(GwbsT gwbsT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwbsT.setGWBSNAME("SRYC" + DateUtils.formatDate(new Date(), "yyyyMMddHHmmssSSS"));
        gwbsT.setGWBSSEQ(new BigDecimal(1));
        gwbsT.setGWBS022(shiroUser.getORG_ID());
        gwbsT.setGWBSENT(shiroUser.getDBC());
        gwbsT.insert();
    }

    @Transactional
    public void edit(GwbsT gwbsT) {
        UpdateWrapper<GwbsT> gwbsTUpdateWrapper = new UpdateWrapper<>();
        gwbsTUpdateWrapper.eq("GWBSENT", gwbsT.getGWBSENT());
        gwbsTUpdateWrapper.eq("GWBSNAME", gwbsT.getGWBSNAME());
        gwbsTUpdateWrapper.eq("GWBSSEQ", gwbsT.getGWBSSEQ());
        if(gwbsT.getGWBS020() == null){
            Dto inDto = Dtos.newDto();
            inDto.put("GWBSENT",gwbsT.getGWBSENT());
            inDto.put("GWBSNAME",gwbsT.getGWBSNAME());
            inDto.put("GWBSSEQ",gwbsT.getGWBSSEQ());
            inDto.put("GWBS020",null);
            this.baseMapper.updateGwbs(inDto);
        }
        gwbsT.update(gwbsTUpdateWrapper);
    }

    @Transactional
    public void delete(GwbsT gwbsT) {
        UpdateWrapper<GwbsT> gwbsTUpdateWrapper = new UpdateWrapper<>();
        gwbsTUpdateWrapper.eq("GWBSENT",gwbsT.getGWBSENT());
        gwbsTUpdateWrapper.eq("GWBSNAME",gwbsT.getGWBSNAME());
        gwbsTUpdateWrapper.eq("GWBSSEQ",gwbsT.getGWBSSEQ());

        gwbsT.delete(gwbsTUpdateWrapper);
    }

    @Transactional
    public void delDataBatch(List<GwbsT> gwbsTList) {
        for(GwbsT gwbsT : gwbsTList){
            UpdateWrapper<GwbsT> gwbsTUpdateWrapper = new UpdateWrapper<>();
            gwbsTUpdateWrapper.eq("GWBSENT",gwbsT.getGWBSENT());
            gwbsTUpdateWrapper.eq("GWBSNAME",gwbsT.getGWBSNAME());
            gwbsTUpdateWrapper.eq("GWBSSEQ",gwbsT.getGWBSSEQ());

            gwbsT.delete(gwbsTUpdateWrapper);
        }
    }

    @Transactional
    public void updateDataBatch(List<GwbsT> gwbsTList) {
        for(GwbsT gwbsT : gwbsTList){
            UpdateWrapper<GwbsT> gwbsTUpdateWrapper = new UpdateWrapper<>();
            gwbsTUpdateWrapper.eq("GWBSENT",gwbsT.getGWBSENT());
            gwbsTUpdateWrapper.eq("GWBSNAME",gwbsT.getGWBSNAME());
            gwbsTUpdateWrapper.eq("GWBSSEQ",gwbsT.getGWBSSEQ());

            gwbsT.update(gwbsTUpdateWrapper);
        }
    }

    @Transactional(readOnly = true)
    public List<Dto> getGwbhList(Dto dto) {
        //客户权限
        dto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(dto));
        //部门权限
        dto.put("ORGS", sysAuthorityService.getOrgsAuthority(dto));
        List<Dto> gwbhDtos = InvokeUtil.invokeBeanMethod("gwbsTMapper,getGwbhList",dto);
        return gwbhDtos;
    }


    @Transactional(readOnly = true)
    public List<GwbsT> getGwbsByGwbkList(Dto dto) {
        //QueryWrapper<GwbsT> gwbsTQueryWrapper = new QueryWrapper<>();
        //gwbsTQueryWrapper.eq("GWBSENT",dto.getString("GWBSENT"));
        //gwbsTQueryWrapper.eq("GWBSDOCDATE",dto.getString("GWBSDOCDATE"));
        //gwbsTQueryWrapper.eq("GWBS001",dto.getString("GWBS001"));
        //gwbsTQueryWrapper.eq("GWBS002",dto.getString("GWBS002"));
        //gwbsTQueryWrapper.eq("GWBS003",dto.getString("GWBS003"));
        //gwbsTQueryWrapper.eq("GWBSSEQ",dto.getString("GWBSSEQ"));
        //List<GwbsT> gwbsDtos = this.baseMapper.selectList(gwbsTQueryWrapper);
        List<GwbsT> gwbsDtos = InvokeUtil.invokeBeanMethod("gwbsTMapper,getGwbsList",dto);
        return gwbsDtos;
    }

    @Transactional
    public void autoGeneration(List<GwbsT> updateList,List<GwbsT> addList) {
        for (GwbsT gwbsT_update : updateList) {
            UpdateWrapper<GwbsT> gwbsTUpdateWrapper = new UpdateWrapper<>();
            gwbsTUpdateWrapper.eq("GWBSENT",gwbsT_update.getGWBSENT());
            gwbsTUpdateWrapper.eq("GWBSNAME",gwbsT_update.getGWBSNAME());
            gwbsTUpdateWrapper.eq("GWBSDOCDATE",gwbsT_update.getGWBSDOCDATE());
            gwbsTUpdateWrapper.eq("GWBSSEQ",gwbsT_update.getGWBSSEQ());
            gwbsT_update.update(gwbsTUpdateWrapper);
        }
        for (GwbsT gwbsT_add : addList) {
            gwbsT_add.insert();
        }
    }

}

